const dataService = require("../services/data");

/**
 * 发布帖子
 * @param {*} ctx
 */
exports.publish = async (ctx) => {
    let datas = ctx.request.body;
    if (!datas) {
        ctx.status = 400;
        ctx.body = {
            code: "02",
            message: "参数异常！"
        };
        return;
    }
    console.log("user", ctx.state.user);
    const { userId } = ctx.state.user;

    await dataService.publish({ ...datas, userId });

    ctx.body = {
        code: "00",
        message: "success"
    };
};
/**
 * 查询我的帖子
 * @param {*} ctx
 */
exports.queryMyPublish = async (ctx) => {
    console.log("user", ctx.state.user);
    const { userId } = ctx.state.user;

    const { success, data } = await dataService.queryMyPublish({ userId });
    if (success) {
        ctx.body = {
            code: "00",
            data,
            message: "success"
        };
    } else {
        ctx.body = {
            code: "01",
            message: "success"
        };
    }
};
/**
 * 查询列表
 * @param {*} ctx
 */
exports.queryList = async (ctx) => {
    const { page, pageSize, channel } = ctx.request.body || {};
    const channels = ["mingyue", "friend_list"];
    if (!channels.includes(channel)) {
        ctx.body = { code: "01", message: "渠道号异常" };
        return;
    }
    console.log("kkkkkkk", page, pageSize);
    if (!page && !pageSize) {
        ctx.body = { code: "01", message: "page或pageSize参数异常！" };
        return;
    }

    const userList = await dataService.queryList({
        page,
        pageSize,
        channel
    });
    const { success, data, message } = userList;
    if (success) {
        ctx.body = { code: "00", data: data };
    } else {
        ctx.body = { code: "01", message };
    }
};
